﻿/********************************************************************************
 * Copyright (C) Newegg Corporation. All rights reserved.
 * 
 * Author: Allen Wang(Allen.G.Wang@newegg.com) 
 * Create Date: 12/23/2008 
 * Description:
 *          
 * Revision History:
 *      Date         Author               Description
 * 
*********************************************************************************/
using System;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
using System.Xml;

namespace NetFramework.Serialization
{
	/// <summary>
	/// NetDataContract序列化。
    /// http://www.cnblogs.com/windwolfreal/archive/2007/09/03/880383.html
	/// </summary>
    internal class NetDataContractSerializerWrapper : SerializerBase
    {
		#region [ Fields ]

		private static ISerializer serializerWrapper = new NetDataContractSerializerWrapper();
		private NetDataContractSerializer serializer = new NetDataContractSerializer();

		#endregion

		#region [ Methods ]

		/// <summary>
		/// 当前实例。
		/// </summary>
		public static ISerializer GetInstance()
		{
			return serializerWrapper;
		}

		#region [ ToBinary ]

		/// <summary>
		/// 把对象转换成二进制数据。
		/// </summary>
		/// <typeparam name="T">要转换成二进制数据对象的类型。</typeparam>
		/// <param name="obj">要转换成二进制数据的对象。</param>
		/// <param name="extraTypes">要序列化的其他对象类型的 Type 数组。</param>
		/// <returns>二进制数据。</returns>
		public override byte[] ToBinary(object obj, Encoding encoding, Type[] extraTypes)
		{
            if (obj == null)
            {
                return null;
            }

            byte[] bytes;
            using (MemoryStream stream = new MemoryStream())
            {
                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, encoding))
                {
                    serializer.WriteObject(writer, obj);
                    writer.Flush();
                    stream.Seek(0, 0);
                    bytes = stream.ToArray();
                }
            }

            return bytes;
		}

		#endregion

		#region [ FromBinary ]

		/// <summary>
		/// 把二进制数据反序列化成对象。
		/// </summary>
		/// <typeparam name="type">要转换成的对象类型。</typeparam>
		/// <param name="bytes">二进制数据</param>
        /// <param name="encoding">编码。</param>
		/// <param name="extraTypes">要序列化的其他对象类型的 Type 数组。</param>
		/// <returns>对象。</returns>
		public override object FromBinary(Type type, byte[] bytes, Encoding encoding, Type[] extraTypes)
		{
            object ret = null;
            if (bytes == null || bytes.Length <= 0)
            {
                return ret;
            }

            using (MemoryStream stream = new MemoryStream(bytes))
            {
                using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, encoding, XmlDictionaryReaderQuotas.Max, null))
                {
                    ret = serializer.ReadObject(reader);
                }
            }

            return ret;
		}

		#endregion

		#endregion
	}
}